//
//  TYHDiscoverController.m
//  OpenPractice
//
//  Created by tanyuehong on 16/2/29.
//  Copyright © 2016年  lijinghua. All rights reserved.
//


#import "OPHomeViewController.h"
#import "OPHomeNavgationBar.h"
#import "TYHLoginController.h"
#import "SDCycleScrollView.h"
#import "TYHImageFuctionView.h"
#import "TYHHomeTitleView.h"
#import "OPHomeGusLikeView.h"
#import "OPHomeViewModel.h"
#import "SCCustomButton.h"
#import "OPEveryDayQustionView.h"
#import "OPHomeGoodClassView.h"
#import "TYHGussLikeTableViewCell.h"
#import <FLAnimatedImage/FLAnimatedImage.h>
#import "NSString+Animation.h"


@interface OPHomeViewController () <SDCycleScrollViewDelegate>

@property (nonatomic,strong) UIView *fuctionView;
@property (nonatomic,strong) UIView *containerView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) OPHomeViewModel *viewModel;
@property (nonatomic,strong) NSMutableArray *gussLikeArr; // 猜你喜欢的
@property (nonatomic,strong) SDCycleScrollView *sliderView;
@property (nonatomic,strong) OPHomeGusLikeView *gessLikeView;
@property (nonatomic,strong) OPEveryDayQustionView *questionView;
@property (nonatomic,strong) OPHomeGoodClassView *goodClass;


@end

@implementation OPHomeViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [self launchAnimation];
}

- (void)launchAnimation  {
    static BOOL isShow = NO;
    if (isShow) {
        return ;
    }
    isShow = YES;
    UIViewController *viewController = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil] instantiateViewControllerWithIdentifier:@"LaunchScreen"];
    UIView *launchView = viewController.view;
    UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
    launchView.frame = [UIApplication sharedApplication].keyWindow.frame;
    [mainWindow addSubview:launchView];
    
    
    NSString *path =  [[NSBundle mainBundle] pathForResource:@"Launche_animation.gif" ofType:nil];
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfFile:path]];
    FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
    imageView.animatedImage = image;
    imageView.animationDuration = 2.0;
    [launchView addSubview:imageView];
    imageView.frame = CGRectMake((self.view.width-320)/2, self.view.height/2 -240, 320, 320);
    [@"虽然辛苦，我还是会选择那种滚烫的人生" animateOnView:imageView atRect:CGRectMake((imageView.width-200)/2, imageView.height-100, 200, 60) forFont:[UIFont systemFontOfSize:16] withColor:[UIColor blackColor] andDuration:2];
    [UIView animateWithDuration:2.0f delay:0.5f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        launchView.layer.transform = CATransform3DScale(CATransform3DIdentity, 1.1f, 1.1f, 1.1f);
    } completion:^(BOOL finished) {
        [launchView removeFromSuperview];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发现";
    self.navgationBar.barType = OPNavgationBarTypeHome;
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.containerView];
    [self.containerView addSubview:self.sliderView];
    [self.containerView addSubview:self.questionView];
    [self.containerView addSubview:self.fuctionView];
    [self.containerView addSubview:self.goodClass];
    [self.containerView addSubview:self.gessLikeView];
    [self layoutMySubview];
    self.view.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    
    [self initResquestData]; // 开始请求网络接口数据
}

- (void)layoutMySubview {
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navgationBar.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    // 设置布局容器约束
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    // 设置轮播视图约束
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.top.equalTo(self.containerView).offset(0);
      make.left.equalTo(self.containerView);
      make.width.mas_equalTo(self.containerView);
      make.height.mas_equalTo(160);
    }];
    
    // 设置每日一题约束
    [self.questionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView);
        make.top.mas_equalTo(self.sliderView.mas_bottom).offset(10);
        make.right.equalTo(self.containerView);
        make.height.equalTo(@70);
    }];
    
    // 设置进度和收藏功能约束
    [self.fuctionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.questionView.mas_bottom).offset(10);
        make.width.equalTo(self.containerView);
        make.height.equalTo(@110);
        make.left.equalTo(self.containerView);
    }];
    
    // 设置推荐课程约束
    [self.goodClass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.fuctionView.mas_bottom).offset(10);
        make.width.left.right.equalTo(self.view);
        make.height.equalTo(@(430));
    }];
    
    // 设置猜你喜欢约束
    [self.gessLikeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodClass.mas_bottom).offset(10);
        make.width.left.right.equalTo(self.view);
        make.height.equalTo(@(400));
    }];
    
    // 最后设置最后一个view的与参照容器view的约束
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.gessLikeView.mas_bottom);
    }];
}

- (void)initResquestData {
    self.viewModel = [[OPHomeViewModel alloc] init];
   // @op_weakify(self);
    [self.viewModel getHomeIndexData:^(NSError *error, id resultObject) {
       // @op_strongify(self);
        //[self loadDataFlashView];
    }];
}

#pragma mark getter and setter mehod

- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        if (@available(iOS 11.0,*)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            self.automaticallyAdjustsScrollViewInsets = NO;
#pragma clang diagnostic pop
        }
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
    }
    return _scrollView;
}

- (void)refresh {
    
}

- (UIView *)containerView {
    if (_containerView == nil) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    }
    return _containerView;
}

- (SDCycleScrollView *)sliderView {
    if (!_sliderView) {
        _sliderView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.width, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        _sliderView.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
        _sliderView.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
        _sliderView.backgroundColor = [UIColor whiteColor];
        _sliderView.imageURLStringsGroup = @[@"slider_default"];
    }
    return _sliderView;
}

- (OPEveryDayQustionView *)questionView {
    if (!_questionView) {
        _questionView = [[OPEveryDayQustionView alloc] init];
        _questionView.imageUrl = @"home_ervery_question";
        _questionView.imageArrowUrl = @"home_qustion_arrow";
        _questionView.qustionName = @"面向对象编程的设计准则有哪些？";
    }
    return _questionView;
}

- (UIView *)fuctionView {
    if (!_fuctionView) {
        _fuctionView = [[UIView alloc] init];
        _fuctionView.backgroundColor = [UIColor whiteColor];
        SCCustomButton *progress = [[SCCustomButton alloc] init];
        [progress setImage:[UIImage imageNamed:@"home_study_progress"] forState:UIControlStateNormal];
        progress.titleLabel.font = [UIFont systemFontOfSize:12];
        [progress setTitle:@"学习进度" forState:UIControlStateNormal];
        progress.imagePosition = SCCustomButtonImagePositionTop;
        [progress setTitleColor:kTYH333333TextColor forState:UIControlStateNormal];
        progress.interTitleImageSpacing = 4;
        [_fuctionView addSubview:progress];
        SCCustomButton *faver = [[SCCustomButton alloc] init];
        [faver setImage:[UIImage imageNamed:@"home_my_faveriter"] forState:UIControlStateNormal];
        faver.titleLabel.font = [UIFont systemFontOfSize:12];
        [faver setTitle:@"我的收藏" forState:UIControlStateNormal];
        faver.interTitleImageSpacing = 4;

        [faver setTitleColor:kTYH333333TextColor forState:UIControlStateNormal];
        faver.imagePosition = SCCustomButtonImagePositionTop;
        [_fuctionView addSubview:faver];
  
        [progress mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@50);
            make.width.equalTo(@80);
            make.height.equalTo(@80);
            make.top.equalTo(@15);
        }];

        [faver mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_fuctionView).offset(-50);
            make.width.equalTo(@80);
            make.height.equalTo(@80);
            make.top.equalTo(@15);
        }];
    }
    return _fuctionView;
}

- (OPHomeGoodClassView *)goodClass {
    if (!_goodClass) {
        _goodClass = [[OPHomeGoodClassView alloc] init];
    }
    return _goodClass;
}

- (OPHomeGusLikeView *)gessLikeView {
    if (!_gessLikeView) {
        _gessLikeView = [[OPHomeGusLikeView alloc] init];
    }
    return _gessLikeView;
}

@end
